package com.example.demo.controller;

import com.example.demo.pojo.Informations;
import com.example.demo.pojo.Replies;
import com.example.demo.service.InformationsService;
import com.example.demo.service.RepliesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
@Controller
public class PhoneController {
    @Autowired
    InformationsService informationsService;
    @Autowired
    RepliesService repliesService;
    @RequestMapping("toshow")
    public String toshow(){
        return "index";
    }
    @RequestMapping("tophoneinfo")
    public String tophoneinfo(int id){
        Informations informations=informationsService.getInformationsById(id);
        informations.setViewcount(informations.getViewcount()+1);
        informationsService.changeInformations(informations);
        return "phoneinfo";
    }
    @RequestMapping("getAllInformations")
    @ResponseBody
    public Object getAllInformations(){
        return informationsService.getAllInformations();
    }
    @RequestMapping("getInformationsbyid")
    @ResponseBody
    public Object getInformationsbyid(Integer id){
        return informationsService.getInformationsById(id);
    }
    @RequestMapping("getInformationsbyinfoid")
    @ResponseBody
    public Object getInformationsbyinfoid(Integer infoid){
        return repliesService.getRepliesByInfoId(infoid);
    }
    @RequestMapping("addreplies")
    @ResponseBody
    public String addreplies(Replies replies){
        Date date=new Date();
        replies.setReplytime(date);
        int num=repliesService.addReplies(replies);
        if (num>0){
            Informations informations=informationsService.getInformationsById(replies.getInfoid());
            informations.setReplycount(informations.getReplycount()+1);
            informations.setLastposttime(date);
            informationsService.changeInformations(informations);
        }
        return num>0?"ok":"on";
    }
}
